<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Helper_Url {

    public static function url_query($parames = array(), $url = null) {
        if ($url === null) {
            return URL::site(Request::current()->uri()) . URL::query($parames);
        } else {
            if (empty($parames)) {
                return $url;
            }
            $a_url = explode('?', $url);
            if (count($a_url) == 1) {
                $query = "";
                foreach ($parames as $key => $value) {
                    $query .= "&$key=$value";
                }
                $query = substr($query, 1);
                return $url = $url . "?" . $query;
            }
            parse_str($a_url[1], $output);
            foreach ($parames as $item_key => $value_item) {
                $has = false;
                foreach ($output as $key_o => $value_o) {
                    if (strtolower($key_o) == strtolower($item_key)) {
                        $output[$item_key] = $value_item;
                        $has = true;
                        break;
                    }
                }
                if (!$has) {
                    $output[$item_key] = $value_item;
                }
            }
            $query = "";
            foreach ($output as $key => $value) {
                $query .= "&$key=$value";
            }
            $query = substr($query, 1);
            return $a_url[0] . "?" . $query;
        }
    }

    public static function full_url() {
        $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
        $sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
        $protocol = substr($sp, 0, strpos($sp, "/")) . $s;
        $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":" . $_SERVER["SERVER_PORT"]);
        return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
    }

    public static function domain_url() {
        $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
        $sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
        $protocol = substr($sp, 0, strpos($sp, "/")) . $s;
        $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":" . $_SERVER["SERVER_PORT"]);
        return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port;
    }

    public static function build_admin_sort($_text, $s_sort_by = null, $default_sort_by = false, $s_sort_dir = 'asc') {
        $_sort_by = Arr::get($_GET, 'sort_by');
        $_sort_dir = Arr::get($_GET, 'sort_dir');

        if ($s_sort_by == null) {
            return $_text;
        }
        if (!empty($_sort_dir)) {
            $s_sort_dir = $_GET['sort_dir'];
        }
        $img_sort = "";
        if (!empty($_sort_by)) {
            $sort_by = $_sort_by;
            if ($s_sort_by === $sort_by) {
                if ($s_sort_dir === 'asc') {
                    $img_sort = '<i class="icon-arrow-down"></i>';
                    $s_sort_dir = "desc";
                } else {
                    $img_sort = '<i class="icon-arrow-up"></i>';
                    $s_sort_dir = "asc";
                }
            }
        } else if ($default_sort_by) {
            $img_sort = '<i class="icon-arrow-down"></i>';
            $s_sort_dir = "desc";
        }

        $html = "<a href='" . self::url_query(array('sort_by' => $s_sort_by, 'sort_dir' => $s_sort_dir)) . "' />$_text $img_sort</a>";
        return $html;
    }

    public static function replace_alias($alias) {

        // default behavior: strip invalid characters and replace spaces with dashes.
        $alias = strip_tags($alias); // strip HTML
        $alias = preg_replace('/[^\.A-Za-z0-9 _-]/', '', $alias); // strip non-alphanumeric characters
        $alias = preg_replace('/\s+/', '-', $alias); // convert white-space to dash
        $alias = preg_replace('/-+/', '-', $alias);  // convert multiple dashes to one
        $alias = trim($alias, '-'); // trim excess
        return $alias;
    }

}

?>
